/**
 * Created by wry on 2015/2/12.
 */
'use strict';

angular.module('backofficeApp.communityService.controllers', []).
    controller('communityGroupCtrl', ['$scope', '$http', '$location', 'CommunityService', function ($scope, $http, $location, communityService) {


        $scope.currentPage = 1;
        $scope.totalPage = 0;
        $scope.pages = [];

        $scope.getCommunityGroupList = function () {

            var params = {
                currentPage : $scope.currentPage
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/communities/getCommunityGroups', params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.resultList = data.data.list;
                $scope.totalPage = data.data.totalPage;
            }).error(function () {
                $scope.resultList = null;
            });
        };

        $scope.delete = function (groupId) {

            var params = {
                groupId: groupId
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/communities/deleteCommunityGroup', params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.getCommunityGroupList();
            }).error(function () {
            });
        };

        $scope.addGroup = function () {
            $location.path('/community/addGroup');
        };

        $scope.update = function (groupId) {
            $location.path('/community/updateGroup/' + groupId);
        };

        $scope.getCommunityGroupList();

        $scope.update = function (groupId) {
            $location.path('/community/updateGroup/' + groupId);
        };

        $scope.findCommunity = function(groupId){
            $location.path('/community/communityByGroup/' + groupId);
        };

        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.getCommunityGroupList();
        }

        $("#box_close").on("click", function () {
            $("#detail_box").css("display", "none");
            $("#mask_box").css("display", "none");
        });

    }]).directive('mutau', function () {
        return {
            restrict: 'E',
            template: '',
            replace: true,
            link: function (scope, element, attrs) {
                scope.$watch('currentPage', function (value) {
                    scope.pages = [];
                    for (var i = value - 5 > 0 ? value - 5 : 1; i <= (value + 5 > scope.totalPage ? scope.totalPage : value + 5); i++) {
                        scope.pages.push(i);
                    }
                });
                scope.$watch('totalPage', function (value) {
                    scope.pages = [];
                    for (var i = scope.currentPage - 5 > 0 ? scope.currentPage - 5 : 1; i <= (scope.currentPage + 5 > value ? value : scope.currentPage + 5); i++) {
                        scope.pages.push(i);
                    }
                });

                scope.isActive = function (page) {
                    return scope.currentPage === page;
                };
                scope.selectPage = function (page) {
                    if (!scope.isActive(page)) {
                        scope.currentPage = page;
                        scope.onSelectPage(page);
                    }
                };
                scope.selectPrevious = function () {
                    if (!scope.noPrevious()) {
                        scope.selectPage(scope.currentPage - 1);
                    }
                };
                scope.selectNext = function () {
                    if (!scope.noNext()) {
                        scope.selectPage(scope.currentPage + 1);
                    }
                };
                scope.noPrevious = function () {
                    return scope.currentPage == 1;
                };
                scope.noNext = function () {
                    return scope.currentPage == scope.totalPage;
                };
            }
        };
    }).controller('addCommunityGroupCtrl', ['$scope', '$http', '$location', 'CommunityService', function ($scope, $http, $location, communityService) {

        $scope.cannel = function () {
            history.go(-1);
        }

        $scope.add = function () {

            var params = {
                groupName: $scope.groupName
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/communities/addCommunityGroup', params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $location.path('/community/group');
            }).error(function () {
            });
        };

    }]).controller('updateCommunityGroupCtrl', ['$scope', '$http', '$location', '$routeParams', function ($scope, $http, $location, $routeParams) {

        $scope.cannel = function () {
            history.go(-1);
        };

        var params = {
            groupId: $routeParams.groupId
        };

        $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/communities/getCommunityGroupById', params, {
            headers: {'Content-Type': 'application/json;charset=utf-8'}
        }).success(function (data) {
            $scope.groupName = data.data.groupName;
        }).error(function () {
        });

        $scope.update = function () {

            var params = {
                groupId: $routeParams.groupId,
                groupName: $scope.groupName
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/communities/updateCommunityGroup', params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function () {
                $location.path('/community/group');
            }).error(function () {
            });
        };

    }]).controller('communityByGroupCtrl', ['$scope', '$http', '$location','$routeParams', 'CommunityService', function ($scope, $http, $location,$routeParams, communityService) {


        $scope.currentPage = 1;
        $scope.totalPage = 0;
        $scope.pages = [];

        $scope.getCommunitiesByGroup = function () {

            var params = {groupId:$routeParams.groupId};

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/communities/getCommunitiesByGroup', params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.resultList = data.data.list;
                $scope.totalPage = data.data.totalPage;
            }).error(function () {
                $scope.resultList = null;
            });
        };

        $scope.delete = function (communityId) {

            var params = {
                groupId: $routeParams.groupId,
                communityId:communityId
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/communities/deleteCommunityGroups', params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function () {
                $scope.getCommunitiesByGroup();
            }).error(function () {
            });
        };

        $scope.back = function () {
            $location.path('/community/group');
        };

        $scope.addGroup = function () {
            $location.path('/community/addCommunity4group/'+$routeParams.groupId);
        };

        $scope.getCommunitiesByGroup();


        $scope.onSelectPage = function (page) {
            $scope.currentPage = page;
            $scope.getCommunitiesByGroup();
        }

        $("#box_close").on("click", function () {
            $("#detail_box").css("display", "none");
            $("#mask_box").css("display", "none");
        });

    }]).directive('mutau', function () {
        return {
            restrict: 'E',
            template: '',
            replace: true,
            link: function (scope, element, attrs) {
                scope.$watch('currentPage', function (value) {
                    scope.pages = [];
                    for (var i = value - 5 > 0 ? value - 5 : 1; i <= (value + 5 > scope.totalPage ? scope.totalPage : value + 5); i++) {
                        scope.pages.push(i);
                    }
                });
                scope.$watch('totalPage', function (value) {
                    scope.pages = [];
                    for (var i = scope.currentPage - 5 > 0 ? scope.currentPage - 5 : 1; i <= (scope.currentPage + 5 > value ? value : scope.currentPage + 5); i++) {
                        scope.pages.push(i);
                    }
                });

                scope.isActive = function (page) {
                    return scope.currentPage === page;
                };
                scope.selectPage = function (page) {
                    if (!scope.isActive(page)) {
                        scope.currentPage = page;
                        scope.onSelectPage(page);
                    }
                };
                scope.selectPrevious = function () {
                    if (!scope.noPrevious()) {
                        scope.selectPage(scope.currentPage - 1);
                    }
                };
                scope.selectNext = function () {
                    if (!scope.noNext()) {
                        scope.selectPage(scope.currentPage + 1);
                    }
                };
                scope.noPrevious = function () {
                    return scope.currentPage == 1;
                };
                scope.noNext = function () {
                    return scope.currentPage == scope.totalPage;
                };
            }
        };
    }).controller('addCommunity4GroupCtrl', ['$scope', '$http', '$location','$routeParams', 'CommunityService', function ($scope, $http, $location,$routeParams, communityService) {
        $scope.cannel = function () {
            history.go(-1);
        };

        $scope.search = function () {

            var params = {
                communityName : $scope.communityName
            }

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/communities/getIsOpenCommunityByName', params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.resultList = data.data;
            }).error(function () {
            });

        };

        $scope.select = function (communityId,communityName) {
            if($('#selCommunity').html == null || $('#selCommunity').html() == ''){
                $('#selCommunity').html(communityName);
            }else{
                $('#selCommunity').html($('#selCommunity').html() + ","+communityName);
            }

            if($scope.communityIds == null){
                $scope.communityIds = communityId;
            }else{
                $scope.communityIds += ","+communityId;
            }

        };

        $scope.add = function () {

            var params = {
                groupName: $scope.groupName
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/communities/addCommunityGroup', params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $location.path('/community/group');
            }).error(function () {
            });
        };

        $scope.save = function () {
            var params = {
                groupId: $routeParams.groupId,
                communityIds:$scope.communityIds
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/communities/insertCommunityGroups', params, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function () {
                $location.path('/community/communityByGroup/' + $routeParams.groupId);
            }).error(function () {
            });
        };

    }]);

